package CCRMClient.GUI;

import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.Color;

import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.JButton;

import org.eclipse.wb.swing.FocusTraversalOnArray;

import CCRMClient.ClientStart;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * This class implements displaying the Customers report
 * @author Ori Ziv
 *
 */
public class CustomersReportScreen extends JPanel {

	private ArrayList<String[]> report;
	private JLabel lblCustomersReport;
	private JScrollPane scpCustReport;
	private JTable tblCustReport;
	private JButton btnGoBack;
	/**
	 * Create the panel.
	 */
	public CustomersReportScreen() {
		initialize();
	}
	private void initialize() {
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		setBackground(Color.WHITE);
		setSize (637, 459);
		setLayout(null);
		add(getLblCustomersReport());
		add(getScpCustReport());
		add(getBtnGoBack());
		setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{getBtnGoBack()}));
	}

	public CustomersReportScreen(ArrayList<String[]> report) {
		this();
		this.report = report;
		loadDataToScreen();
	}

	private void loadDataToScreen() {
		if (!report.isEmpty()) {
			DefaultTableModel dtm = (DefaultTableModel) tblCustReport.getModel();
			dtm.removeRow(0);
			Object[] tblRow = new Object[5];
			for (int i = 0 ; i < report.size() ; i ++) {
				String[] row = report.get(i);
				tblRow[0] = row[0];
				tblRow[1] = row[1];
				tblRow[2] = row[2];
				tblRow[3] = row[3];
				tblRow[4] = row[4];
				dtm.addRow(tblRow);
			}
		}
		tblCustReport.setEnabled(true);
	}
	
	JLabel getLblCustomersReport() {
		if (lblCustomersReport == null) {
			lblCustomersReport = new JLabel("Customers Report");
			lblCustomersReport.setHorizontalAlignment(SwingConstants.CENTER);
			lblCustomersReport.setForeground(Color.RED);
			lblCustomersReport.setFont(new Font("David", Font.BOLD, 26));
			lblCustomersReport.setBackground(Color.WHITE);
			lblCustomersReport.setBounds(0, 0, 637, 49);
		}
		return lblCustomersReport;
	}
	
	JScrollPane getScpCustReport () {
		if (scpCustReport == null) {
			scpCustReport = new JScrollPane();
			scpCustReport.setBounds(10, 53, 587, 317);
			scpCustReport.setViewportView(getTblCustReport());
		}
		return scpCustReport;
	}
	
	JTable getTblCustReport() {
		if (tblCustReport == null) {
			tblCustReport = new JTable();
			tblCustReport.setRowSelectionAllowed(false);
			tblCustReport.setFont(new Font("David", Font.BOLD, 11));
			tblCustReport.setModel(new DefaultTableModel(
				new Object[][] {
					{null, null, null, null, null},
				},
				new String[] {
					"ID", "Name", "Total", "Buys", "Rate"
				}
			) {
				Class[] columnTypes = new Class[] {
					String.class, String.class, String.class, String.class, String.class
				};
				public Class getColumnClass(int columnIndex) {
					return columnTypes[columnIndex];
				}
				boolean[] columnEditables = new boolean[] {
					false, false, false, false, false
				};
				public boolean isCellEditable(int row, int column) {
					return columnEditables[column];
				}
			});
		}
		return tblCustReport;
	}
	JButton getBtnGoBack() {
		if (btnGoBack == null) {
			btnGoBack = new JButton("Go Back");
			btnGoBack.addActionListener(new BtnGoBackActionListener());
			btnGoBack.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnGoBack.setBounds(10, 394, 124, 23);
		}
		return btnGoBack;
	}
	/**
	 * This inner class implements event listener for pressing Go Back button
	 * @author Ori Ziv
	 *
	 */
	private class BtnGoBackActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			ClientStart.panelSwicher(new ReportsScreen());
		}
	}
}
